var calendar_obj='';
function getCalendar(obj){
	calendar_obj=obj;
	createSelectYear();  //创建年份下拉,并给对应事件
	createMonthSelect();  //创建月份下拉，并给对应事件
	
	//根据年，月，用table绘制日历。 年月变动则 重新绘制
	createTabledate(parseInt($(".aboluo-calendar-select-year").html()),parseInt(getMonthNumber($(".aboluo-calendar-month").html())));
}

//阻止冒泡
function stopBubble(e){
	if(e && e.stopPropagation){// 别的浏览器
		e.stopPropagation();
	}else{ //IE
		window.event.cancelBubble=true;
	}
}	
//定义了yearselect并赋值,且添加事件，选择则对应的table日期也将改变,且已选中日期会跳到当前选择月的日期，然后给右边明细栏赋值
function createSelectYear(){
	var Nowtime=new Date();
	var currYear=Nowtime.getFullYear();
	$(".aboluo-calendar-select-year").html(currYear);
}
//创建月的下拉框，并赋值,且添加事件，选择则对应的table日期也将改变,且已选中日期会跳到当前选择月的日期，然后给右边明细栏赋值
function createMonthSelect(){
	var Nowtime=new Date();
	var currMonth=Nowtime.getMonth();
	$(".aboluo-calendar-month").html(getMonthEnglish(currMonth+1));
}
//月份数字
function getMonthNumber(currMonth){
	switch (currMonth){
		case "January":
			return 1;
			break;
		case "February":
			return 2;
			break;
		case "March":
			return 3;
			break;
		case "April":
			return 4;
			break;
		case "May":
			return 5;
			break;
		case "June":
			return 6;
			break;
		case "July":
			return 7;
			break;
		case "August":
			return 8;
			break;
		case "September":
			return 9;
			break;
		case "October":
			return 10;
			break;
		case "November":
			return 11;
			break;
		case "December":
			return 12;
			break;
		default:
			break;
	}
}
//英文版的月份
function getMonthEnglish(currMonth){
	switch (currMonth){
		case 1:
			return "January";
			break;
		case 2:
			return "February";
			break;
		case 3:
			return "March";
			break;
		case 4:
			return "April";
			break;
		case 5:
			return "May";
			break;
		case 6:
			return "June";
			break;
		case 7:
			return "July";
			break;
		case 8:
			return "August";
			break;
		case 9:
			return "September";
			break;
		case 10:
			return "October";
			break;
		case 11:
			return "November";
			break;
		case 12:
			return "December";
			break;
		default:
			break;
	}
}


//根据传入的年月，创建对应的table日期,并且在每个td中创建a标签用于事件，与样式内边框的设置
function createTabledate(year,yue){
	var rilitabledele=$(".aboluo-rilitbody");
	if(rilitabledele!="" && rilitabledele!=null && rilitabledele!='undefined'){
	//rilitabledele.parentNode.removeChild(rilitabledele);
		rilitabledele.remove();
	}
	var rilitable=newElement('tbody');
	rilitable.setAttribute("class","aboluo-rilitbody");
	var rili=$(".aboluo-rilitable");
	rili.append(rilitable);
	//先得到当前月第一天是星期几,然后根据这个星期算前面几天的上个月最后几天.
	var date=setdateinfo(year,yue,1);
	var weekday=date.getDay();
	var pervLastDay;
	if(weekday!=0){
		pervLastDay=weekday-1;
	}else{
		pervLastDay=weekday+6;
	}
	//得到上个月最后一天;
	var pervMonthlastDay=getPervMonthLastDay(year,yue);
	//上月最后几天循环
	var lastdays=pervMonthlastDay-pervLastDay+1;
	var tr=newElement('tr');
	for(var i=lastdays;i<=pervMonthlastDay;i++){
		var td=newElement("td");
		var a=getA(parseInt(yue)-1==0?parseInt(year)-1:year,parseInt(yue)-1==0?12:parseInt(yue)-1,i);
		a.style.color="#BFBFC5";
		td.appendChild(a);
		td.setAttribute("class","aboluo-pervMonthDays");
		tr.appendChild(td);
	}
	//这个月开始的循环
	var startDays=8-weekday==8?1:8-weekday;
	for(var i=1;i<=startDays;i++){
		var td=newElement("td");
		var b=getA(year,yue,i);
		td.appendChild(b);
		tr.appendChild(td);
	}
	rilitable.appendChild(tr);
	//指定年月最后一天
	var currMonthLashDay=getCurrMonthLashDay(year,yue);
	//当月除开第一行的起点
	var currmonthStartDay=currMonthLashDay-(currMonthLashDay-startDays)+1;
	//当月还剩余的天数
	var syts=currMonthLashDay-startDays;
	//循环次数
	var xhcs=0;
	if(check(syts/7)){
	//是小数
	xhcs=Math.ceil(syts/7);//向上取整
	}else{
	xhcs=syts/7;	
	}
	
	//这是下个月开始的变量;
	var jilvn=1;
	for(var i=0;i<xhcs;i++){
		var tr1=newElement('tr');
		for(var n=1;n<=7;n++){
			var td=newElement('td');
			if(startDays==0){
				var c=getA(parseInt(yue)+1==parseInt(13)?parseInt(year)+1:year,parseInt(yue)+1==parseInt(13)?1:parseInt(yue)+1,jilvn);
				c.style.color="#BFBFC5";
				td.appendChild(c);
				td.setAttribute("class","aboluo-nextMonthDays");
				jilvn++;
				tr1.appendChild(td);
				continue;
			}else{
			startDays++;
			var d=getA(year,yue,startDays);
			td.appendChild(d);
				if(startDays==currMonthLashDay){
					startDays=0;
				}
			tr1.appendChild(td);	
			}
		
		}
		rilitable.appendChild(tr1);
	}
	setA(); //设置td中a的事件

}



//给上一个月最后几天点击跳转月份
function pervA(year,yue,day){
	createTabledate(year,yue);  //创建对应的table(日期)
	updateSelect(year,yue);    //改变年月select值
}

//给上一个月最后几天点击跳转月份
function nextA(year,yue,day){
	createTabledate(year,yue);
	updateSelect(year,yue);
}

function updateSelect(year,yue){	
	$(".aboluo-calendar-month").html(getMonthEnglish(parseInt(yue)));
	$(".aboluo-calendar-select-year").html(year);
}


//给rightdiv创建元素并赋值，根据传入的年月日给内部的元素赋值 ,月份是 1-12
function setRigth(year,yue,day,index){
	//alert(1)
	
}

function formatByYearyueday(year,yue,day){
	year=year.toString();
	yue=yue.toString();
	day=day.toString();
	return year+"-"+yue+"-"+day;
}

function formatByDate(date){
	date=date.substring(0,10);
	var daxx=date.toString().split("-");
	return daxx[0]+"-"+(daxx[1].length<2?'0'+daxx[1]:daxx[1])+"-"+(daxx[2].length<2?'0'+daxx[2]:daxx[2]);
}

//给tbody中的td中的A设置事件，上个月的天数,这个月的天数,下个月的天数三种对应的事件
//这里还有个功能就是判断当前的A中日期是不是数据库中有带状态的日期,如果是就给相当的样式
function setA(){
	var tbody=$(".aboluo-rilitbody");
	var arr=$(".aboluo-rilitbody a");
	arr.on('tap',function(){
		var datearr=$(this).attr("date").split("-");
		var parentClassName=$(this).parent().attr("class");
		if(parentClassName=="aboluo-pervMonthDays"){
			pervA(datearr[0],datearr[1],datearr[2]);
		}else if(parentClassName=="aboluo-nextMonthDays"){
			nextA(datearr[0],datearr[1],datearr[2]);
		}else{
			if($(this).hasClass('hasSelect')){
				$(".aboluo-rilitbody a").removeClass('active');
				$(this).addClass('active');
				$('.delivery .time-range').show();

				del_date=datearr[2]+'/'+datearr[1]+'/'+datearr[0];
				$('.time-range .orange-font em').html(del_date);
				var bvs_del_win=calendar_obj.bvs_del_win;
				for(var i=0;i<bvs_del_win.length;i++) {
					if (bvs_del_win[i] == del_date) {
						var bvc_del_win_range=calendar_obj.bvc_del_win_range;
						var bvc_del_win_range_str='';
						for(var j=0;j<bvc_del_win_range.length;j++){
							var bvs_ava_ind=bvc_del_win_range[j].bvs_ava_ind;
							if(bvs_ava_ind[i]=="Y"){
								bvc_del_win_range_str+='<label class="usable">'+bvc_del_win_range[j].bs_del_win_per+'</label>';
							}else{
								bvc_del_win_range_str+='<label>'+bvc_del_win_range[j].bs_del_win_per+'</label>';
							}
						}
						$('.time-range .time-section').html(bvc_del_win_range_str);


						$('.time-section label').on('tap',function(){
							if($(this).hasClass('usable')){
								$('.time-section label').removeClass('active');
								$(this).addClass('active');
								del_win_per=$(this).html();
							}
						})
						break;
					}
				}
			}
		}
		stopBubble(this);
		
	})
}



//获取当前选取的日期
function getAclickDomDate(){
	var aclick=$(".aboluo-aclick");
	if(aclick==""){
		//说明没选,那么就给当天,按12月算
		var date=new Date();
		return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
	}else{
		return aclick.getAttribute("date");
	}
}

//获取当前选中的a元素
function getAclickDom(){
	var aclick=$(".aboluo-aclick");
	if(aclick==""){
		//说明没选,那么就给当天,按12月算
		return $(".aboluo-currToday");
	}else{
		return aclick;
	}
}


//创建元素
function newElement(val){
	return document.createElement(val);
}

//创建date对象并赋值
function setdateinfo(year,yue,day){
	var date=new Date();
	date.setFullYear(parseInt(year));
	date.setMonth(parseInt(yue)-1);
	date.setDate(parseInt(day));
	return date;
}

//根据年月日判断是不是星期六星期天 //yue 按12算
function isweekend(year,yue,day){
	var date=new Date();
	date.setFullYear(year);
	date.setMonth(yue-1);
	date.setDate(day);
	var week=date.getDay();
	if(week==0 || week==6){
		return true;
	}
	return false;
}

//判断c是否是小数
function check(c){
	var r=/^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
	return r.test(c);
}

//得到指定月的上个月最后一天传进来按 12月算
function getPervMonthLastDay(year,yue){
	//当月就是  yue-1 也就是计算机里面的0-11月份,那么算上个月的最后一天就是当月的0天
	return parseInt(new Date(year,yue-1,0).getDate());
}

//得到指定月最后一天 传进来按 12月算
function getCurrMonthLashDay(year,yue){
	if(yue>=12){
		year=year+1;
		yue=yue-12;
	}
	return parseInt(new Date(year,yue,0).getDate());
}


//创建a标签并设置公用属性
function getA(year,yue,day){
	var date=new Date();
	var nowMonth=date.getMonth()+1;
	var nowDay=date.getDate();
	var a=newElement("a");
	a.href="javascript:void(0)";
	a.innerHTML=day;
	a.setAttribute("class","gray");
	a.setAttribute("date",year+"-"+(yue < 10 ? ('0'+yue) : yue )+"-"+ (day <10 ? '0'+day : day));


	var aDay_date=new Date(year + '/' + yue + '/' + day + " 00:00:00").getTime();
	var bvs_del_win=calendar_obj.bvs_del_win;
	for(var i=0;i<bvs_del_win.length;i++) {
		var detail_day = bvs_del_win[i].split("/");
		var dy_date = new Date(detail_day[2] + '/' + detail_day[1] + '/' + detail_day[0] + " 00:00:00").getTime();
		if (aDay_date == dy_date) {
			a.setAttribute("class","hasSelect");
		}
	}
	return a;
}

$(".aboluo-month-a-next").on('tap',function(){
	var monthselect=$(".aboluo-calendar-month");
	var monthvalue=parseInt(getMonthNumber(monthselect.html()));
	
	var yearselect=$(".aboluo-calendar-select-year");
	var yearvalue=parseInt(yearselect.html());
	if(monthvalue==12){
		yearvalue+=1;
		//这里已经变了一年了,所以就要根据年重读数据
//			getjjrszModelByYear(yearvalue);
		monthvalue=1;
	}else{
		monthvalue+=1;
	}
	monthselect.html(getMonthEnglish(monthvalue));
	yearselect.html(yearvalue);
	var aclick=$(".aboluo-aclick");
	createTabledate(yearselect.html(),getMonthNumber(monthselect.html()));
	
})
$(".aboluo-month-a-perv").on('tap',function(){
	var monthselect=$(".aboluo-calendar-month");
	var monthvalue=parseInt(getMonthNumber(monthselect.html()));
	var yearselect=$(".aboluo-calendar-select-year");
	var yearvalue=parseInt(yearselect.html())
	if(monthvalue==1){
		yearvalue-=1;
		//这里已经变了一年了,所以就要根据年重读数据
//			getjjrszModelByYear(yearvalue);
		monthvalue=12;
	}else{
		monthvalue-=1;
	}
	monthselect.html(getMonthEnglish(monthvalue));
	yearselect.html(yearvalue);
	var aclick=$(".aboluo-aclick");
	createTabledate(yearselect.html(),getMonthNumber(monthselect.html()));
})

